package cn.edu.neu.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.neu.model.Store;
import cn.edu.neu.model.Systemset;
import cn.edu.neu.model.User;
import cn.edu.neu.service.StoreService;
import cn.edu.neu.service.SystemsetService;
import cn.edu.neu.service.UserService;


@Controller
public class StoreAction {
	
	@Autowired
	private StoreService sService;
	@Autowired
	private UserService userService;

	/**
	 * @author Chaos
	 * 跳转余额
	 */
	@RequestMapping("/balance")
	public String Balance(Model model,HttpSession session) {
		Map<String, Object> userMap=(Map<String, Object>) session.getAttribute("LOGINUSER");
		 String id=(String) userMap.get("id");
		/* System.out.println(id);*/
		List<User> yue=userService.MyYuE(id);
		 model.addAttribute("yue", yue);
		return "/Store/balance";
	}
	
	/**
	 * @author Chaos
	 * 跳转店面收款
	 */
	@RequestMapping("/storereceipts")
	public String Storereceipts(Model model) {
		List<Store> shous=sService.getStorereceipts();
		model.addAttribute("shous",shous);
		
		return "/Store/storereceipts";
	}
	
	/**
	 * @author Chaos
	 *提款记录
	 */
	@RequestMapping("/Record")
	public String Record(Model model,HttpSession session) {
		Map<String, Object> userMap=(Map<String, Object>) session.getAttribute("LOGINUSER");
		 String id=(String) userMap.get("id");
		/* System.out.println(id);*/
		List<Store> dpsk=sService.DPSK(id);
		 model.addAttribute("dpsk", dpsk);
		return "/Store/record";
	}
	
	/**
	 * @author Chaos
	 *店铺收款
	 */
	@RequestMapping("/shoustorereceipts")
	public String ShouKuan(Store store,Systemset systemset,User user,HttpSession session) {
		String sid=store.getSid();
		/*System.out.println(sid);*/
        Map<String, Object> userMap=(Map<String, Object>) session.getAttribute("LOGINUSER");
		
		String uid=(String) userMap.get("id");
		String unit=systemset.getUnit();
		String money=store.getTikuan();
		//可以方便地修改日期格式
		Date now = new Date(); 
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String update_time = dateFormat.format(now); 
		String uuid = UUID.randomUUID().toString();
		/*System.out.println(update_time);*/
		/*获取商店名称*/
		String sname=sService.selectsname(sid);
		store.setCash_unit(unit);
		store.setMember_id(uid);
		store.setCr_id(uuid);
		store.setName(sname);
		store.setCash_time(update_time);
		
		if(unit.equals("金豆"))
		{
			sService.TiJD(store);
			user.setId(uid);
			user.setUmoney(money);
			user.setUpdate_time(update_time);
			userService.JiaJD(user);
			
			sService.incashrecord(store);
			return "/Store/storereceipts";
			
		}
		if(unit.equals("地币"))
		{
			sService.TiDB(store);
			user.setId(uid);
			user.setUmoney(money);
			user.setUpdate_time(update_time);
			userService.JiaDB(user);
			return "/Store/storereceipts";
			
		}
		return "login";
		
		
	}
	
	
}
